package com.example.rabbitmq7.controller;

import com.example.rabbitmq7.constant.Constants;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RequestMapping("producer")
@RestController
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Resource(name = "transactionRabbitTemplate")
    private RabbitTemplate transactionRabbitTemplate;
    @RequestMapping("delay")
    public String delay() throws InterruptedException {
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","rabbitmq delay1,发送时间:" + new Date());
        Thread.sleep(1000);
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","rabbitmq delay2,发送时间:" + new Date());
        Thread.sleep(1000);
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal","rabbitmq delay3,发送时间:" + new Date());
        return "消息发送成功";
    }

    @RequestMapping("delay2")
    public String delay2(){
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal2","rabbitmq delay,发送时间" + new Date(),message -> {
            message.getMessageProperties().setExpiration("20000");
            return message;
        });
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,"normal2","rabbitmq delay,发送时间" + new Date(),message -> {
            message.getMessageProperties().setExpiration("10000");
            return message;
        });
        return "消息发送成功";
    }

    @RequestMapping("delay3")
    public String delay3(){
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","rabbitmq delay,发送时间" + new Date(),message -> {
            message.getMessageProperties().setDelay(20000);
            return message;
        });
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,"delay","rabbitmq delay,发送时间" + new Date(),message -> {
            message.getMessageProperties().setDelay(10000);
            return message;
        });
        return "消息发送成功";
    }

    @Transactional
    @RequestMapping("/trans")
    public String trans() {
        transactionRabbitTemplate.convertAndSend(Constants.TRANS_EXCHANGE,"trans","rabbitmq trans1");
        //制造异常
        int ret = 3/0;
        transactionRabbitTemplate.convertAndSend(Constants.TRANS_EXCHANGE,"trans","rabbitmq trans2");
        return "消息发送成功";
    }

    @RequestMapping("/qos")
    public String qos() {
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE,"qos","rabbitmq qos" + i);
        }
        return "消息发送成功";
    }
}
